using System;
using System.Collections.Generic;
using UnityEngine;

public class SocialPlatformsManager : MonoBehaviour
{
	public static SocialPlatformsManager instance;

	[HideInInspector]
	private string scoreLeaderboardID;

	private string surviveTimeLeaderboardID;

	private string killCountboardID;

	private bool IsAuthenticated
	{
		get
		{
			return Social.localUser.authenticated;
		}
	}

	public bool isLogedin
	{
		get
		{
			return PlayerPrefs.GetInt("Logedin", 0) == 1;
		}
		set
		{
			PlayerPrefs.SetInt("Logedin", value ? 1 : 0);
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}

	private void OnApplicationPause(bool paused)
	{
		if (paused)
		{
			isLogedin = IsAuthenticated;
		}
	}

	private void Start()
	{
		scoreLeaderboardID = "CgkIiuGWsZQTEAIQAA";
		surviveTimeLeaderboardID = "CgkIiuGWsZQTEAIQAQ";
		killCountboardID = "CgkIiuGWsZQTEAIQAg";
		if (isLogedin)
		{
			Authenticate();
		}
	}

	public void Authenticate(Action<bool> callback = null)
	{
		if (IsAuthenticated)
		{
			return;
		}
		Social.localUser.Authenticate(delegate(bool success)
		{
			if (success)
			{
				isLogedin = true;
			}
			if (callback != null)
			{
				callback(success);
			}
		});
	}

	public void SendHighscore(int score, Action<bool> callback = null)
	{
		if (IsAuthenticated)
		{
			Social.ReportScore(score, scoreLeaderboardID, delegate
			{
			});
		}
	}

	public void SendBestSurviveTime(int surviveTime, Action<bool> callback = null)
	{
		if (IsAuthenticated)
		{
			Social.ReportScore(surviveTime, surviveTimeLeaderboardID, delegate
			{
			});
		}
	}

	public void SendKillCount(int killCount, Action<bool> callback = null)
	{
		if (IsAuthenticated)
		{
			Social.ReportScore(killCount, killCountboardID, delegate
			{
			});
		}
	}

	public void ShowLeaderboard()
	{
		if (IsAuthenticated)
		{
			SendDatasToStore();
			Social.ShowLeaderboardUI();
			return;
		}
		Authenticate(delegate(bool authResult)
		{
			if (authResult)
			{
				SendDatasToStore();
				Social.ShowLeaderboardUI();
			}
		});
	}

	public void ShowAchievements()
	{
		if (IsAuthenticated)
		{
			Social.ShowAchievementsUI();
			return;
		}
		Authenticate(delegate(bool auth)
		{
			if (auth)
			{
				Social.ShowAchievementsUI();
			}
		});
	}

	public void ReportProgress(string achievemetId, float progress)
	{
		if (!IsAuthenticated)
		{
			return;
		}
		Social.ReportProgress(achievemetId, progress, delegate(bool sended)
		{
			if (sended)
			{
				Debug.Log("achivement progress : SUCCESS");
			}
			else
			{
				Debug.Log("achivement progress : FAIL");
			}
		});
	}

	public void SendDatasToStore()
	{
		SendHighscore(ScoreManager.BestMass);
		SendBestSurviveTime((int)ScoreManager.BestSurviveTime);
		SendKillCount(ScoreManager.BestKill);
	}

	private void ReportListedAchievements(Dictionary<string, int> dict, int count)
	{
		foreach (KeyValuePair<string, int> item in dict)
		{
			if (item.Value <= count)
			{
				ReportProgress(item.Key, 100f);
			}
		}
	}
}
